//
//  HEDoctorHeadCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEDoctorHeadCell.h"

@interface HEDoctorHeadCell ()

@property (nonatomic, strong) UIImageView *goodsimgView;
@property (nonatomic, strong) QMUIGhostButton *craeBtn;


@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *addLb;

@property (nonatomic, strong) UILabel *deptLb;
@property (nonatomic, strong) UILabel *hospLb;

@property (nonatomic, strong) UILabel *tagLb;

@property (nonatomic, strong) UILabel *descLb;
@property (nonatomic, strong) UILabel *intoLb;

@property (nonatomic, strong) UILabel *ratioLb;
@property (nonatomic, strong) UILabel *priceLb;

@property (nonatomic, strong) NSMutableArray *rateArr;
@end

@implementation HEDoctorHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIImageView *warp = [UIImageView new];
    warp.image = IMAGE_NAMED(@"doctor_bk");
    warp.contentMode = UIViewContentModeScaleAspectFill;
    warp.clipsToBounds = YES;
    warp.userInteractionEnabled = YES;
    [self.contentView addSubview:warp];

    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(0);
        make.left.equalTo(self.contentView).mas_offset(0);
        make.right.equalTo(self.contentView).mas_offset(0);
//        make.height.mas_greaterThanOrEqualTo(245);
        make.bottom.equalTo(self.contentView).mas_offset(0);
    }];
    
    // 头像
    _goodsimgView = [UIImageView new];
    _goodsimgView.image = IMAGE_NAMED(@"noImage");
    _goodsimgView.contentMode = UIViewContentModeScaleAspectFill;
    [warp addSubview:_goodsimgView];
    
    // 关注
    _craeBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
    _craeBtn.titleLabel.font = DYBlodFont(15);
    [_craeBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [warp addSubview:_craeBtn];
    [_craeBtn addTarget:self action:@selector(careBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 标签   我的医生 \ 关注
    _tagLb = [UILabel new];
    _tagLb.text = @"我的医生";
    _tagLb.backgroundColor = KHexColor(@"F2791C");
    _tagLb.textColor = KWhiteColor;
    _tagLb.font = DYBlodFont(14);
    _tagLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:_tagLb];
    [_tagLb hg_setAllCornerWithCornerRadius: 3];
    
    _tagLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *tagTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagClick)];
    [_tagLb addGestureRecognizer:tagTap];
    

    // 姓名
    _titleLb = [UILabel new];
    _titleLb.text = @"张三";
    _titleLb.font = DYBlodFont(18);
    [warp addSubview:_titleLb];
    
    // 申请加号
    _addLb = [UILabel new];
    _addLb.text = @"申请加号";
    _addLb.font = DYBlodFont(16);
    _addLb.backgroundColor = CNavBgColor;
    _addLb.textColor = KWhiteColor;
    _addLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:_addLb];
    [_addLb hg_setAllCornerWithCornerRadius: 4];
    _addLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *addTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addClick)];
    [_addLb addGestureRecognizer:addTap];

    // 科室
    _deptLb = [UILabel new];
    _deptLb.text = @"副主任医师 | 老年病科";
    _deptLb.font = DYFont(15);
    _deptLb.textColor = CNavLightBlackColor;
    [warp addSubview:_deptLb];

    // 所属医院
    _hospLb = [UILabel new];
    _hospLb.text = @"中南大学湘雅医院";
    _hospLb.font = DYFont(15);
    _hospLb.textColor = CNavLightBlackColor;
    [warp addSubview:_hospLb];

    
    // 描述
    _descLb = [UILabel new];
    _descLb.text = @"中南大学湘雅医院中南大学湘雅医院中南大学湘雅医院中南大学湘雅医院中南大学湘雅医院中南大学湘雅医院";
    _descLb.font = DYFont(15);
    _descLb.textColor = CNavLightBlackColor;
    _descLb.numberOfLines = 2;
    _descLb.userInteractionEnabled = YES;
    [warp addSubview:_descLb];
    
    
    _intoLb = [UILabel new];
    _intoLb.text = @"进入医生主页>";
    _intoLb.font = DYFont(15);
    _intoLb.textColor = CNavBgColor;
    _intoLb.backgroundColor = KWhiteColor;
    _intoLb.userInteractionEnabled = YES;
    [_descLb addSubview:_intoLb];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toSignClick)];
    [_intoLb addGestureRecognizer:tap];
    
    [_intoLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(_descLb.mas_right);
       make.bottom.equalTo(_descLb.mas_bottom);
    }];
    
    [_goodsimgView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(warp).mas_offset(15);
       make.left.equalTo(warp).mas_offset(15);
       make.width.height.mas_offset(90);
    }];
    [_goodsimgView hg_setAllCornerWithCornerRadius: 45];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_goodsimgView.mas_top);
       make.left.equalTo(_goodsimgView.mas_right).mas_offset(11);
    }];
    [_titleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_titleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

    
    [_addLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLb);
        make.left.equalTo(_titleLb.mas_right).mas_offset(10);
        make.width.mas_offset([@"申请加号" widthForFont:_addLb.font] + 10);
        make.height.mas_offset([@"申请加号" heightForFont:_addLb.font width:[@"申请加号" widthForFont:_addLb.font]] + 5);
    }];
        
    [_craeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLb);
        make.right.equalTo(warp).mas_offset(-10);
        make.width.mas_offset([@"+关注" widthForFont:_craeBtn.titleLabel.font] + 20);
        make.height.mas_offset([@"+关注" heightForFont:_craeBtn.titleLabel.font width:[@"+关注" widthForFont:_craeBtn.titleLabel.font]] + 10);
    }];
    
    [_tagLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_titleLb);
        make.right.equalTo(warp).mas_offset(-10);
        make.width.mas_offset([@"我的医生" widthForFont:_tagLb.font] + 18);
        make.height.mas_offset([@"我的医生" heightForFont:_tagLb.font width:[@"我的医生" widthForFont:_tagLb.font]] + 10);
    }];
    
    [_deptLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
       make.left.equalTo(_titleLb.mas_left);
    }];
  
    [_hospLb mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_deptLb.mas_bottom).mas_offset(10);
       make.left.equalTo(_titleLb.mas_left);
    }];
    
 
    [_descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_goodsimgView.mas_bottom).mas_offset(10);
        make.left.equalTo(_goodsimgView.mas_left);
        make.right.equalTo(warp.mas_right).mas_offset(-10);
    }];
    
    
    NSArray *titls = @[@"服务人数", @"接诊率", @"好评率"];
    _rateArr = @[].mutableCopy;
    
    for (int i = 0; i < titls.count; i++) {
        HERateView *itemWarp = [HERateView new];
        [itemWarp setupTitle:titls[i] value:@"0"];
        [self.contentView addSubview:itemWarp];
        [_rateArr addObject:itemWarp];
    }
    [_rateArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_descLb.mas_bottom).mas_offset(20);
        make.bottom.equalTo(warp.mas_bottom).mas_offset(-10);
    }];
    [_rateArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
}

#pragma mark -
- (void)careBtnClick {
    if ([_delegate respondsToSelector:@selector(changeCareStatus:)]) {
        [_delegate changeCareStatus: YES];
    }
}

- (void)tagClick {
    
    if ([@"2" isEqualToString: self.formModel.doc_details.user_doc_collection]) {
        if ([_delegate respondsToSelector:@selector(changeCareStatus:)]) {
            [_delegate changeCareStatus: NO];
        }
    }
}

- (void)addClick {
    if ([_delegate respondsToSelector:@selector(toApplyPlusPage)]) {
        [_delegate toApplyPlusPage];
    }
}

- (void)toSignClick {
    if ([_delegate respondsToSelector:@selector(toDoctorMainPage)]) {
        [_delegate toDoctorMainPage];
    }
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    
    _delegate = formModel.proxyVC;
    [_goodsimgView sd_setImageWithURL:[NSURL URLWithString: formModel.doc_details.avatar] placeholderImage: IMAGE_NAMED(@"hmang")];
    
    _titleLb.text = formModel.doc_details.realname;
    _deptLb.text = formModel.doc_details.KDeptStr;
    _hospLb.text = formModel.doc_details.hospital_name;
    _descLb.text = formModel.doc_details.KSkilful;

    HERateView *itemWarp0 = _rateArr[0];
    [itemWarp0 setupValue: [NSString stringWithFormat:@"%ld", formModel.doc_details.server_total]];
    
    HERateView *itemWarp1 = _rateArr[1];
    [itemWarp1 setupValue: formModel.doc_details.KapprovalRate];
    
    HERateView *itemWarp2 = _rateArr[2];
    [itemWarp2 setupValue: formModel.doc_details.KsatifRate];
    
    
    if ([@"1" isEqualToString:formModel.doc_details.user_doc_flag] ) {
        _tagLb.hidden = NO;
        _craeBtn.hidden = YES;
        
        _tagLb.text = @"我的医生";
        _tagLb.backgroundColor = formModel.doc_details.KTagColor;
        _tagLb.userInteractionEnabled = NO;
    }else if ([@"2" isEqualToString:formModel.doc_details.user_doc_collection] ) {
        _tagLb.hidden = NO;
        _craeBtn.hidden = YES;
        _tagLb.text = @"已关注";
        _tagLb.backgroundColor = CNavBgColor;
        _tagLb.userInteractionEnabled = YES;
    }
    
    else{
        _tagLb.hidden = YES;
        _craeBtn.hidden = NO;
    }
    
    // 注意顺序
    [self pageType];
}

- (void)pageType {
    if (self.formModel.doc_pageType == 1) {
        _addLb.hidden = YES;
        _tagLb.userInteractionEnabled = NO;
        _craeBtn.hidden = YES;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface HERateView ()

@property (nonatomic, strong) UILabel *numLb;
@property (nonatomic, strong) UILabel *itemLb;

@end

@implementation HERateView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    UILabel *numLb = [UILabel new];
    _numLb = numLb;
    numLb.text = @"0";
    numLb.font = DYBlodFont(17);
    numLb.textAlignment = NSTextAlignmentCenter;
    [self addSubview:numLb];
    
    UILabel *itemLb = [UILabel new];
    _itemLb = itemLb;
    itemLb.text = @"-";
    itemLb.font = DYFont(16);
    itemLb.textAlignment = NSTextAlignmentCenter;
    itemLb.textColor = CNavDargGrayColor;
    [self addSubview:itemLb];
    
    [numLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self);
    }];
    [itemLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numLb.mas_bottom).mas_offset(5);
        make.right.left.equalTo(self);
        make.bottom.equalTo(self).mas_offset(-10);
    }];
}

- (void)setupTitle:(NSString *)title value:(NSString *)value {
    _itemLb.text = title;
    _numLb.text = value;
}

- (void)setupValue:(NSString *)value {
    _numLb.text = value;
}

@end
